#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QHostInfo>
#include<QHostAddress>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString str="www.baidu.com";
    ui->lineEdit->setText(str);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString MainWindow::ProtocolTypeName(QAbstractSocket::NetworkLayerProtocol protocoltype)
{
    switch (protocoltype) {
    case QAbstractSocket::IPv4Protocol:
        return "IPv4 Protocol";

    case QAbstractSocket::IPv6Protocol:
        return "IPv6 Protocol";

    case QAbstractSocket::AnyIPProtocol:
        return "Any IP Protocol";

    default:
         return "Unknown Network Layer Protocol";

    }
}

void MainWindow::LookupHostInfoFunc(const QHostInfo &infos)
{
   QList<QHostAddress>list= infos.addresses();
   for(QHostAddress address:list)
   {
       ui->textEdit->append("你所查询的主机地址:"+ui->lineEdit->text());
       ui->textEdit->append("协议类型:"+ProtocolTypeName(address.protocol()));
       ui->textEdit->append("本地IP地址:"+address.toString());
   }
}

//查询域名或IP地址
void MainWindow::on_pushButton_2_clicked()
{
    QString strhostname=ui->lineEdit->text();
    QHostInfo::lookupHost(strhostname,this,SLOT(LookupHostInfoFunc(QHostInfo)));
}

//清除数据信息
void MainWindow::on_pushButton_clicked()
{
    ui->textEdit->clear();
}
